﻿#region Imports

using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

#endregion

namespace IEC.ColumnOrdering.SP2007.Deployment
{

    /// <summary>
    /// Receives management requests for email features.
    /// </summary>
    [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    public sealed class EmailFeatureReceiver : ApplicationFeatureReceiver
    {

        #region Methods - Instance Member

        #region Methods - Instance Member - SPFeatureReceiver Members

        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">
        /// An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object 
        /// that represents the properties of the event.
        /// </param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            this.SetEmailEnabledProperty(properties, true);
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">
        /// An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object 
        /// that represents the properties of the event.
        /// </param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            this.SetEmailEnabledProperty(properties, false);
        }

        /// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">
        /// An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object 
        /// that represents the properties of the event.
        /// </param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            // TODO: implement
        }

        /// <summary>
        /// Occurs when a Feature is uninstalled.
        /// </summary>
        /// <param name="properties">
        /// An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object 
        /// that represents the properties of the event.
        /// </param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            // TODO: implement
        }

        /// <summary>
        /// Sets the email enabled propertybag value.
        /// </summary>
        /// <param name="properties">
        /// The properties collection received from  the feature.
        /// </param>
        /// <param name="isEnabled">
        /// <c>true</c> if email should be enabled,
        /// otherwise <c>false</c>.
        /// </param>
        private void SetEmailEnabledProperty(SPFeatureReceiverProperties properties, bool isEnabled)
        {
            SPWeb web = WebContextHelper.GetCurrentWeb(properties);

            bool originalUnsafeUpdateState = web.AllowUnsafeUpdates;

            try
            {
                web.AllowUnsafeUpdates = true;

                // TODO: specify valid key; string.Empty is a placeholder herein
                web.AllProperties[string.Empty] = isEnabled.ToString();

                web.Update();
            }
            finally
            {
                web.AllowUnsafeUpdates = originalUnsafeUpdateState;
            }
        }

        #endregion

        #endregion

    }

}
